package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.clients.IUserClient;
import com.hmall.clients.ItemClient;
import com.hmall.order.common.MQConstants;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderService;
import com.hmall.pojo.Address;
import com.hmall.pojo.Item;
import com.hmall.pojo.User;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * @Author Mingxi Guo
 * @Create 2022年7月17日11:53:10
 */

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired
    private IUserClient iUserClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ItemClient itemClient;



    /**
     * 提交订单
     *
     * @param requestParams
     * @return
     */
//    @GlobalTransactional
    @Override
    public Long shop(RequestParams requestParams) {
        //插入数据到Order表
        Long itemId = requestParams.getItemId();
        Item item = itemClient.getById(itemId);

        if (item.getStock()- requestParams.getNum()<0) {
            throw new RuntimeException("库存不足");
        }
        Order order = new Order();
        long totalFee = item.getPrice() * requestParams.getNum();
        order.setTotalFee(totalFee)
                .setPaymentType(requestParams.getPaymentType())
                .setUserId(2L)
                .setStatus(1)
                .setCreateTime(new Date());

        orderMapper.insert(order);
        //插入数据到orderDetail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId())
                .setItemId(requestParams.getItemId())
                .setNum(requestParams.getNum())
                .setName(item.getName())
                .setSpec(item.getSpec())
                .setPrice(totalFee)
                 .setImage(item.getImage())
                 ;
        orderDetailMapper.insert(orderDetail);
         //插入数据到OrderLogistics表中
        Address address = iUserClient.findAddressById(Long.valueOf(requestParams.getAddressId()));
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics
                .setOrderId(order.getId())
                .setLogisticsCompany("顺丰")
                .setLogisticsNumber(UUID.randomUUID().toString().replaceAll("-","").substring(0,8))
                .setContact(address.getContact())
                .setPhone(address.getMobile())
                .setProvince(address.getProvince())
                .setCity(address.getCity())
                .setStreet(address.getStreet())
                 .setTown(address.getTown());
        orderLogisticsMapper.insert(orderLogistics);
        //库存扣减
        int stock = item.getStock() - requestParams.getNum();
        item.setStock(stock);
        itemClient.update(item);
         //向延迟队列发送延迟30分钟的消息
        rabbitTemplate.convertAndSend(MQConstants.DELAY_EXCHANGE
                ,MQConstants.DELAY_ROUTING_KEY,orderDetail.getId(),message -> {
            message.getMessageProperties().setDelay(1000 * 60 * 30);
            return  message;
                });
        return order.getId();
    }
}
